package com.share.config.redisson;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

@Configuration
@ConditionalOnClass(RedissonClient.class)
public class RedissonConfig {

    @Autowired
    private RedisProperties redisProperties;

    @Bean
    RedissonClient redissonClient() {
        Config config = new Config();

        //1.设置地址
        String singleAddress = "redis://" + redisProperties.getHost() + ":" + redisProperties.getPort();
        config.useSingleServer().setAddress(singleAddress);

        //2.设置db
        if (redisProperties.getDatabase() != null) {
            config.useSingleServer().setDatabase(redisProperties.getDatabase());
        }

        //2.设置密码
        if (!StringUtils.isEmpty(redisProperties.getPassword())) {
            config.useSingleServer().setPassword(redisProperties.getPassword());
        }

        return Redisson.create(config);
    }
}
